package com.internetCafes.spms.web.customer.service.impl.customer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.web.customer.common.cont.CommonConst;
import com.internetCafes.spms.web.customer.common.cont.CustomerConst;
import com.internetCafes.spms.web.customer.entity.customer.Customer;
import com.internetCafes.spms.web.customer.entity.customer.CustomerFlow;
import com.internetCafes.spms.web.customer.entity.customer.CustomerQuali;
import com.internetCafes.spms.web.customer.mapper.customer.CustomerMapper;
import com.internetCafes.spms.web.customer.model.customer.*;
import com.internetCafes.spms.web.customer.model.customer.quali.CustomerQualiDo;
import com.internetCafes.spms.web.customer.model.customer.quali.CustomerQualiPo;
import com.internetCafes.spms.web.customer.service.customer.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.internetCafes.spms.web.toolbox.entity.QualiConf;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户信息表 服务实现类
 * </p>
 *
 * @author Zj
 * @since 2021-10-20
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    private ICustomerCooperationService cooperationService;
    @Autowired
    private ICustomerFlowService flowService;
    @Autowired
    private ICustomerFollowService followService;
    @Autowired
    private ICustomerMaterialService materialService;
    @Autowired
    private ICustomerQualiService qualiService;
    @Autowired
    private ICustomerQualiImageService qualiImageService;


    @Override
    public void updateYearDealFlag() {
        baseMapper.updateYearDealFlag();
    }

    @Override
    public List<String> repeatCodeList(List<String> certificateCodeList) {
        return list(new LambdaQueryWrapper<Customer>()
                .in(Customer::getCertificateCode, certificateCodeList)
                .eq(Customer::getDelFlag, CommonConst.FlagEnum.NOT.getId()))
                .stream().map(Customer::getCertificateCode).collect(Collectors.toList());
    }

    @Override
    public List<Customer> unFollowCustomerList() {
        return baseMapper.unFollowCustomerList();
    }

    @Override
    public List<CustomerQualiPo> customerQualiPoList(CustomerQualiDo customerQualiDo) {
        return baseMapper.customerQualiPoList(customerQualiDo);
    }

    @Override
    public boolean saveDoBatch(List<Customer> customerDoList) {
        /*
         * 批量保存客户信息
         * 保存客户资质列表
         * 新增资质创建跟进
         */
        boolean result = saveBatch(customerDoList);
        if (!result) {
            return false;
        }

        List<CustomerQuali> qualiList = new ArrayList<>();

        for (Customer customer : customerDoList) {
            if (CollectionUtil.isNotEmpty(customer.getQualiIdList())) {
                for (Long qualiId: customer.getQualiIdList()) {
                    qualiList.add(new CustomerQuali().setCustomerId(customer.getId()).setQualiId(qualiId));
                }
            }
        }

        if (CollectionUtil.isNotEmpty(qualiList)) {
            qualiService.saveBatch(qualiList);
        }

        // 创建记录
        List<CustomerFlow> flowBatchDo = customerDoList.stream().map(
                        customer -> {
                            CustomerFlow flow = new CustomerFlow();
                            flow.setCustomerId(customer.getId())
                                    .setActionTag(CustomerConst.FlowActionTag.CREATE.getId())
                                    .setTargetUserId(ShiroUtils.getUserId())
                                    .setTargetUserName(ShiroUtils.getUserEntity().getNickName());
                            return flow;
                        })
                .collect(Collectors.toList());
        flowService.saveBatch(flowBatchDo);
        return true;
    }

    @Override
    public List<CustomerDetailPo> customerPoDetailList(CustomerDetailListDo customerDetailListDo) {
        return baseMapper.customerPoDetailList(customerDetailListDo);
    }

    @Override
    public List<QualiConf> qualiConfList(Long customerId) {
        return baseMapper.qualiConfList(customerId);
    }

    @Override
    public CustomerDetailPo customerPoDetail(CustomerDetailDo customerDetailDo) {
        return baseMapper.customerPoDetail(customerDetailDo);
    }

    @Override
    public Page<CustomerPagingPo> customerPoPaging(Page<Customer> page, CustomerPagingDo customerPagingDo) {
        // customerPagingDo.setTenantId(ShiroUtils.getUserTenantId());
        return baseMapper.customerPoPaging(page, customerPagingDo);
    }

    @Override
    public boolean deleteByIdList(List<Long> idList) {
        return update(new LambdaUpdateWrapper<Customer>()
                .in(Customer::getId, idList)
                .set(Customer::getDelFlag, CommonConst.FlagEnum.IS.getId()));
    }

    @Override
    public boolean clearCustomerInfo(List<Long> idList) {
        // 删除资质图片信息
        qualiImageService.removeByCustomerIdList(idList);

        // 删除资质列表信息
        qualiService.removeByCustomerIdList(idList);

        // 删除流转记录信息
        flowService.removeByCustomerIdList(idList);

        // 删除跟进日志记录
        followService.removeByCustomerIdList(idList);

        // 删除报考资料记录
        materialService.removeByCustomerIdList(idList);

        // 删除合作记录
        cooperationService.removeByCustomerIdList(idList);
        return true;
    }

    @Override
    public List<CustomerPagingPo> customerPoList(CustomerPagingDo customerPagingDo) {
        return baseMapper.customerPoList(customerPagingDo);
    }
}
